package B刷题.二叉树;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * @author wei.zhao
 * @description: 剑指 Offer 55 - I.二叉树的深度
 * @date 2022年11月10日 14:01
 */
public class Code05_MaxDepth {

    public int maxDepth(TreeNode root) {
        int result = 0;
        if (null == root) {
            return result;
        }
        LinkedList<TreeNode> list = new LinkedList<>();
        HashMap<TreeNode, Integer> levelMap = new HashMap<>();
        list.add(root);
        levelMap.put(root, 1);
        result = 1;
        while (!list.isEmpty()) {
            TreeNode pop = list.pop();
            Integer level = levelMap.get(pop);
            if (null != pop.left) {
                list.add(pop.left);
                levelMap.put(pop.left, level + 1);
                result = level + 1;
            }
            if (null != pop.right) {
                list.add(pop.right);
                levelMap.put(pop.right, level + 1);
                result = level + 1;
            }
        }
        return result;
    }
    
}
